package client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import server.BkCAccITF;
import server.PersonInterface;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import client.Configuration;


public class BkUserRegisterGUI extends JFrame {

	private JPanel contentPane;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	
	Configuration config=new Configuration();
	String rmiAddress=config.getRmiAddress();
	BkCAccITF CAitf;
    Person ps = new Person();
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BkUserRegisterGUI frame = new BkUserRegisterGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
	private JTextField textField_1;
	
	public BkUserRegisterGUI() {
		setTitle("\u7528\u6237\u6CE8\u518C");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("\u8BF7\u8BBE\u7F6E\u60A8\u7684\u57FA\u672C\u4FE1\u606F\uFF1A");
		label.setFont(new Font("??????", Font.PLAIN, 16));
		label.setBounds(41, 31, 221, 27);
		contentPane.add(label);
	
		
		passwordField = new JPasswordField();
		passwordField.setBounds(183, 121, 153, 27);
		contentPane.add(passwordField);
		
		
		JLabel label_2 = new JLabel("\u767B\u5F55\u5BC6\u7801\uFF1A");
		label_2.setFont(new Font("??????", Font.PLAIN, 15));
		label_2.setBounds(100, 123, 84, 21);
		contentPane.add(label_2);
		
		JButton button = new JButton("\u6CE8\u518C");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					CAitf = (BkCAccITF) Naming.lookup(rmiAddress+"BankCurrentAccount");
					String name = textField_1.getText();
					String skey = passwordField.getText();
					
					if(skey.equals("")||name.equals(""))
						JOptionPane.showMessageDialog(null, "输入不能为空!");
					else{
						int ID = ps.getPID();
				  	    int key = Integer.parseInt(skey);
				  	    int CANum = CAitf.addAcc(ID, name, key);
			            if( CANum==-1)
			            	JOptionPane.showMessageDialog(null, "此ID已注册，请直接登录。");
			            else
			            	JOptionPane.showMessageDialog(null, "注册成功！您的活期账户号为"+CANum);
					   
					BkUserRegisterGUI.this.dispose();
					HomePageGUI frame = new HomePageGUI();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true); 
					}
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
			}
		});
		button.setFont(new Font("??????", Font.PLAIN, 17));
		button.setBounds(164, 180, 109, 45);
		contentPane.add(button);
		
		JLabel label_3 = new JLabel("\u59D3\u540D\uFF1A");
		label_3.setFont(new Font("??????", Font.PLAIN, 15));
		label_3.setBounds(100, 90, 74, 21);
		contentPane.add(label_3);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(183, 85, 153, 27);
		contentPane.add(textField_1);
		
		JButton button_1 = new JButton("\u8FD4\u56DE");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BkUserLoginGUI frame = new BkUserLoginGUI();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				dispose();
			}
		});
		button_1.setFont(new Font("??????", Font.PLAIN, 13));
		button_1.setBounds(341, 10, 83, 22);
		contentPane.add(button_1);
		
	}

}
